package com.examsys.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.examsys.po.PaperSection;
import com.examsys.service.PaperSectionService;
import com.examsys.service.PaperService;
import com.examsys.util.JsonUtils;

@WebServlet("/paperSection")
public class PaperSectionController extends BaseController{

	private Logger log=Logger.getLogger(this.getClass());
	
	private PaperSectionService paperSectionService;
	
	private PaperService paperService;
	
	/**
	 * 初始化工作，这里主要是给业务逻辑层属性做初始化
	 */
	@Override
	public void init() throws ServletException {
		super.init();
		paperSectionService=lookUp("paperSectionService");
		paperService=lookUp("paperService");
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void addSave(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String paperId = request.getParameter("paper_id");
		Integer paper_id=null;
		if(paperId!=null&&!"".equals(paperId)){
			paper_id=Integer.valueOf(paperId);
		}
		String section_name = request.getParameter("section_name");
		String remark = request.getParameter("remark");
		Integer per_score = Integer.valueOf(request.getParameter("per_score"));
		
		PaperSection paperSection=new PaperSection();
		log.info(paperSection);
		
		paperSection.setSection_name(section_name);
		paperSection.setPer_score(per_score);
		paperSection.setRemark(remark);
		
		Map jsonDatas=new HashMap();//存放json数据的集合
		jsonDatas.put("status", 0);//默认状态为0，表示操作失败
		try {
			if(paper_id!=null){
				paperSection.setPaper(this.paperService.get(paper_id));
			}else{
				paperSection.setPaper(null);
			}
				
			boolean flag = paperSectionService.add(paperSection);
			if(flag){
				jsonDatas.put("status", 1);//设置状态为1，表示操作成功
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
	}
	
	/**
	 * 初始化修改
	 * @param id
	 * @throws IOException 
	 */
	public void update(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Integer id=Integer.valueOf(request.getParameter("id"));
		log.info("开始初始化编号为"+id+"的系统功能数据供前端修改");//把id放到日志
		PaperSection paperSection=null;
		try {
			paperSection=paperSectionService.get(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(paperSection));
	}
	
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void updateSave(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String paperId = request.getParameter("paper_id");
		Integer paper_id=null;
		if(paperId!=null&&!"".equals(paperId)){
			paper_id=Integer.valueOf(paperId);
		}
		
		String id = request.getParameter("id");
		Integer idd=null;
		if(id!=null&&!"".equals(id)){
			idd=Integer.valueOf(id);
		}
		
		String section_name = request.getParameter("section_name");
		String remark = request.getParameter("remark");
		Integer per_score = Integer.valueOf(request.getParameter("per_score"));
		Map jsonDatas=new HashMap();//存放json数据的集合
		jsonDatas.put("status", 0);//默认状态为0，表示操作失败
		try {
			PaperSection oldpaperSection=paperSectionService.get(idd);
			if(paper_id!=null){
				oldpaperSection.setPaper(this.paperService.get(paper_id));
			}else{
				oldpaperSection.setPaper(null);
			}
			oldpaperSection.setSection_name(section_name);
			oldpaperSection.setPer_score(per_score);
			oldpaperSection.setRemark(remark);
			
			log.info(oldpaperSection);
			
			boolean flag = paperSectionService.update(oldpaperSection);
			if(flag){
				jsonDatas.put("status", 1);//设置状态为1，表示操作成功
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
	}
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Integer id=Integer.valueOf(request.getParameter("id"));
		log.info(id);
		Map jsonDatas=new HashMap();//存放json数据的集合
		jsonDatas.put("status", 0);//默认状态为0，表示操作失败
		try {
			boolean flag = paperSectionService.delete(id);
			if(flag){
				jsonDatas.put("status", 1);//设置状态为1，表示操作成功
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		log.info("进入系统功能管理页面");
		request.getRequestDispatcher("paperSection/PaperSectionManager.jsp").forward(request, response);
	}
	
}





